package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.SmsHomeRecommendProduct;
import com.itheima.mapper.SmsHomeRecommendProductMapper;
import com.itheima.service.SmsHomeRecommendProductService;
import com.itheima.util.PageBean;
import com.itheima.util.PageBeanVo;
import com.itheima.util.PageUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;

/**
 * <p>
 * 人气推荐商品表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class SmsHomeRecommendProductServiceImpl extends ServiceImpl<SmsHomeRecommendProductMapper, SmsHomeRecommendProduct> implements SmsHomeRecommendProductService {
    @Autowired
    private SmsHomeRecommendProductMapper recommendProductMapper;

    @Override
    public PageBean list(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page<SmsHomeRecommendProduct> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeRecommendProduct> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(productName)){
            queryWrapper.like(SmsHomeRecommendProduct::getProductName,productName);
        }
        if (recommendStatus != null){
            queryWrapper.eq(SmsHomeRecommendProduct::getRecommendStatus,recommendStatus);
        }
        Page<SmsHomeRecommendProduct> smsHomeRecommendProductPage = recommendProductMapper.selectPage(pageInfo, queryWrapper);
        PageBean pageBean = PageUtils.getPageBean(smsHomeRecommendProductPage);
        return pageBean;
    }

    @Override
    public int create(List<SmsHomeRecommendProduct> recommendProducts) {
        for (SmsHomeRecommendProduct recommendProduct : recommendProducts) {
            recommendProductMapper.insert(recommendProduct);
        }
        return 1;
    }

    @Override
    public int delete(List<Long> ids) {
        return recommendProductMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateStatus(List<Long> ids, Integer recommendStatus) {
        List<SmsHomeRecommendProduct> list = recommendProductMapper.selectBatchIds(ids);
        for (SmsHomeRecommendProduct smsHomeRecommendProduct : list) {
            smsHomeRecommendProduct.setRecommendStatus(recommendStatus);
            recommendProductMapper.updateById(smsHomeRecommendProduct);
        }
        return 1;
        // return recommendProductMapper.updateStatus(ids,recommendStatus);
    }

    @Override
    public int updateSort(Integer sort, Integer id) {
        SmsHomeRecommendProduct smsHomeRecommendProduct = recommendProductMapper.selectById(id);
        smsHomeRecommendProduct.setSort(sort);
        return recommendProductMapper.updateById(smsHomeRecommendProduct);
    }
}
